package net.gz01.wallet.web.model.base;

import com.alibaba.fastjson.JSONObject;
import net.gz01.util.ZODate;
import net.gz01.wallet.message.NetSignReq;
import net.gz01.wallet.message.RealCompanyReq;
import net.gz01.wallet.message.RealCompanyResp;
import net.gz01.wallet.web.config.SystemEnum;
import org.apache.commons.codec.binary.Base64;

@SuppressWarnings("serial")
public class FddOrder extends BaseFddOrder<FddOrder> {
    public static final FddOrder dao = new FddOrder().dao();

    public FddOrder selectByUserCode(String userCode){

        return super.findFirst("select * from fdd_order where userCode='" + userCode + "'" );
    }

    //boolean isSign: true：代表查询网签交易，  false：代表查询实名认证交易
    public FddOrder selectByUserCode(boolean isSign, String userCode){
        final String type = "3";
        String sql = "";
        if(isSign){
            sql = "type=" + type;
        }else {
            sql = "type<>" + type;
        }
        return super.findFirst("select * from fdd_order where userCode='" + userCode + "'"  +
                " AND "  + sql);
    }


    public FddOrder selectByOrder(boolean isSign, String customerId, String orderNo){
        final String type = "3";
        String sql = "";
        if(isSign){
             sql = "type=" + type;
        }else {
            sql = "type<>" + type;
        }
        return super.findFirst("select * from fdd_order where fddOrderNo='" + orderNo + "'" + " AND " +
                sql + " AND " + "customer_id='" + customerId + "'");
    }


    public FddOrder selectByOrder(boolean isSign,String orderNo){
        final String type = "3";
        String sql = "";
        if(isSign){
            sql = "type=" + type;
        }else {
            sql = "type<>" + type;
        }
        return super.findFirst("select * from fdd_order where fddOrderNo='" + orderNo + "'" + " AND " +
                sql);
    }

    public void createOrder(RealCompanyReq req, RealCompanyResp resp){
        this.setCustomerId(resp.getCustomer_id());
        this.setFddOrderNo(resp.getTransactionNo());
        this.setUserCode(req.getOpen_id());
        this.setType(req.getAccount_type());
        this.setCreateTime(ZODate.getDateTime());
        this.setNotifyUrl(req.getNotify_url());
        this.setReturnUrl(req.getReturn_url());
        this.setUrl(Base64.encodeBase64String(resp.getUrl().getBytes()));
        this.setResCode(SystemEnum.OrderStatus.InProcess.getValue());
        this.save();
    }

    public void createOrderSign(NetSignReq req, String resUrl){
        this.setCustomerId(req.getCustomer_id());
        this.setFddOrderNo(req.getTransaction_id());
        this.setUserCode(req.getOpen_id());
        //contract_id
        this.setContractId(req.getContract_id());//合同编号
//        this.set
        this.setType("3");
        this.setCreateTime(ZODate.getDateTime());
        this.setNotifyUrl(req.getNotify_url());
        this.setReturnUrl(req.getReturn_url());
        this.setUrl(Base64.encodeBase64String(resUrl.getBytes()));
        this.setResCode(SystemEnum.OrderStatus.InProcess.getValue());
        this.save();
    }

    public void createOrderSignAuto(NetSignReq req, JSONObject resUrl){
        this.setCustomerId(req.getCustomer_id());
        this.setFddOrderNo(req.getTransaction_id());
        this.setUserCode(req.getOpen_id());
        //contract_id
        this.setContractId(req.getContract_id());//合同编号
//        this.set
        this.setType("3");
        this.setCreateTime(ZODate.getDateTime());
        this.setNotifyUrl(req.getNotify_url());
        this.setReturnUrl(req.getReturn_url());
        this.setJsondata(resUrl.toJSONString());
        this.setFinishTime(ZODate.getDateTime());
        this.setResCode(SystemEnum.OrderStatus.Success.getValue());
        this.save();
    }

}
